package ccm.component.menu;

import ccm.common.Buffer;

import javax.swing.*;
import java.awt.*;

class Int64MenuItemJPanel extends NumberMenuItemJPanel
{
    protected Int64MenuItemJPanel(DelegateSenderInterface sender,Component[] rightMenu,String saveArgPrefix,int dataIndex,boolean canModify,ScnsType type,int page,String name)
    {
        super(sender,rightMenu,saveArgPrefix,dataIndex,canModify,type,page,name);
        setModel(this.valueJSpinner);
    }

    @Override
    protected boolean sameAsValue(Object v){return sameAsValue(value,v);}

    @Override
    public void decodeValueAndSet(byte[] receiveBuf,int start){decodeValueAndSet(this::setValue,receiveBuf,start);}

    @Override
    public int setBuf(byte[] buf,int start){return setBuf(buf,start,valueJSpinner);}

    protected static void setModel(JSpinner valueJSpinner)
    {
        valueJSpinner.setModel(new SpinnerNumberModel(((Number)valueJSpinner.getValue()).longValue(),Long.MIN_VALUE,Long.MAX_VALUE,1));
    }

    protected static boolean sameAsValue(Object v1,Object v2)
    {
        return v1 instanceof Number&&v2 instanceof Number&&((Number)v1).longValue()==((Number)v2).longValue();
    }

    protected static void decodeValueAndSet(SetValueInterface setValue,byte[] receiveBuf,int start)
    {
        setValue.setValue(Buffer.getInt64(receiveBuf,start));
    }

    protected static int setBuf(byte[] buf,int start,JSpinner valueJSpinner)
    {
        return Buffer.setInt64(buf,start,((Number)valueJSpinner.getValue()).longValue());
    }
}
